// pages/diet-record/diet-record.js
Page({
  data: {
    currentDate: '',
    dietRecords: [
      {
        id: 1,
        time: '08:00',
        mealType: '早餐',
        foods: [
          { name: '鸡蛋', weight: 60, calories: 93 },
          { name: '牛奶', weight: 200, calories: 100 },
          { name: '面包', weight: 80, calories: 200 }
        ],
        totalCalories: 393
      },
      {
        id: 2,
        time: '12:30',
        mealType: '午餐',
        foods: [
          { name: '米饭', weight: 150, calories: 195 },
          { name: '红烧肉', weight: 100, calories: 300 },
          { name: '青菜', weight: 100, calories: 20 }
        ],
        totalCalories: 515
      },
      {
        id: 3,
        time: '18:30',
        mealType: '晚餐',
        foods: [
          { name: '面条', weight: 200, calories: 220 }
        ],
        totalCalories: 220
      }
    ],
    showAddModal: false,
    newRecord: {
      time: '',
      mealType: '早餐',
      foodName: '',
      foodWeight: '',
      foodCalories: ''
    }
  },

  onLoad() {
    console.log('饮食记录页面加载');
    this.setCurrentDate();
  },

  onShow() {
    // 页面显示时的逻辑
  },

  // 设置当前日期
  setCurrentDate() {
    const now = new Date();
    const dateStr = `${now.getFullYear()}-${String(now.getMonth() + 1).padStart(2, '0')}-${String(now.getDate()).padStart(2, '0')}`;
    this.setData({
      currentDate: dateStr
    });
  },

  // 显示添加记录弹窗
  showAddRecordModal() {
    this.setData({
      showAddModal: true
    });
  },

  // 隐藏添加记录弹窗
  hideAddRecordModal() {
    this.setData({
      showAddModal: false,
      newRecord: {
        time: '',
        mealType: '早餐',
        foodName: '',
        foodWeight: '',
        foodCalories: ''
      }
    });
  },

  // 输入框变化
  onInput(e) {
    const field = e.currentTarget.dataset.field;
    const value = e.detail.value;
    
    this.setData({
      [`newRecord.${field}`]: value
    });
  },

  // 选择餐次类型
  onMealTypeChange(e) {
    const value = e.detail.value;
    this.setData({
      'newRecord.mealType': value
    });
  },

  // 添加饮食记录
  addDietRecord() {
    const { time, mealType, foodName, foodWeight, foodCalories } = this.data.newRecord;
    
    if (!time || !mealType || !foodName || !foodWeight || !foodCalories) {
      wx.showToast({
        title: '请填写完整信息',
        icon: 'error'
      });
      return;
    }
    
    const foodWeightNum = parseFloat(foodWeight);
    const foodCaloriesNum = parseFloat(foodCalories);
    
    if (isNaN(foodWeightNum) || isNaN(foodCaloriesNum)) {
      wx.showToast({
        title: '重量和热量必须是数字',
        icon: 'error'
      });
      return;
    }
    
    const newRecord = {
      id: this.data.dietRecords.length + 1,
      time: time,
      mealType: mealType,
      foods: [
        {
          name: foodName,
          weight: foodWeightNum,
          calories: foodCaloriesNum
        }
      ],
      totalCalories: foodCaloriesNum
    };
    
    this.setData({
      dietRecords: [...this.data.dietRecords, newRecord],
      showAddModal: false,
      newRecord: {
        time: '',
        mealType: '早餐',
        foodName: '',
        foodWeight: '',
        foodCalories: ''
      }
    });
    
    wx.showToast({
      title: '添加成功',
      icon: 'success'
    });
  },

  // 删除饮食记录
  deleteRecord(e) {
    const id = e.currentTarget.dataset.id;
    wx.showModal({
      title: '确认删除',
      content: '确定要删除这条饮食记录吗？',
      success: (res) => {
        if (res.confirm) {
          const dietRecords = this.data.dietRecords.filter(record => record.id !== id);
          this.setData({
            dietRecords: dietRecords
          });
          
          wx.showToast({
            title: '删除成功',
            icon: 'success'
          });
        }
      }
    });
  },

  // 底部导航切换
  switchTab(e) {
    const tab = e.currentTarget.dataset.tab;
    
    switch (tab) {
      case 'home':
        wx.redirectTo({
          url: '/pages/index/index'
        });
        break;
      case 'history':
        wx.redirectTo({
          url: '/pages/history/history'
        });
        break;
      case 'plan':
        wx.redirectTo({
          url: '/pages/plan/plan'
        });
        break;
      case 'profile':
        wx.redirectTo({
          url: '/pages/profile/profile'
        });
        break;
      default:
        wx.redirectTo({
          url: '/pages/index/index'
        });
    }
  }
})